package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.constant.CacheKey;
import cc.yugu.yhdw.common.domain.hdw.Agency;
import com.whalin.MemCached.MemCachedClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 代理商服务切面类
 *
 * @Date:Created in 2018/7/5 16:15
 * @Author:
 * @Modified By: zjn 2018/7/5 16:15
 */
@Component
@Aspect
public class AgencyServiceAspect {

    @Autowired
    MemCachedClient memCachedClient;

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.hdw.AgencyService.findInfo(..))")
    private void findInfo() {}
    /**
    * 查询，设置缓存
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/5  16:17
    */
    @Around("findInfo()")
    public Object findInfo(ProceedingJoinPoint joinPoint) throws Throwable {
        Integer id = (Integer) joinPoint.getArgs()[0];
        String key = CacheKey.key(CacheKey.K_ID_V_LEAGUE_INFO, id);

        Object result = memCachedClient.get(key);
        if(result == null) {
            result = joinPoint.proceed();
            if(result != null) {
                memCachedClient.set(key, result);
            }
        }

        return result;
    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.hdw.AgencyService.update(..))")
    private void update() {}
    /**
    * 更新
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/5  16:18
    */
    @Around("update()")
    public Object update(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        Agency agency = (Agency) joinPoint.getArgs()[0];
        String key = CacheKey.key(CacheKey.K_ID_V_LEAGUE_INFO, agency.getId());
        memCachedClient.delete(key);
        return result;
    }

}
